home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / hf^k-2.dms / in.adf / MUIClass.Lha / Include / Classes / TWiMUI / Misc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-18  |  9.9 KB  |  308 lines

  1. //
  2. //  $VER: Misc.h        1.2 (31 Aug 1996)
  3. //
  4. //    c 1996 Thomas Wilhelmi
  5. //
  6. //
  7. // Address : Taunusstrasse 14
  8. //           61138 Niederdorfelden
  9. //           Germany
  10. //
  11. //  E-Mail : willi@twi.rhein-main.de
  12. //
  13. //   Phone : +49 (0)6101 531060
  14. //   Fax   : +49 (0)6101 531061
  15. //
  16. //
  17. //  $HISTORY:
  18. //
  19. //  16 Jun 1996 :   1.0 : first public Release
  20. //
  21. //  27 Jul 1996 :   1.1 : Neu:
  22. //                        - template <class T> T &TWiArrayList<T>::operator= (const T &);
  23. //                        - TWiFormat &operator= (const TWiFormat &);
  24. //
  25. //  31 Aug 1996 :   1.2 : Neu:
  26. //                        - Klasse TWiShare
  27. //                        Änderungen:
  28. //                        - Alle Vergleichsoperatoren für die Klasse TWiStr
  29. //                          wurden 'inline' und 'const' definiert.
  30. //
  31.  
  32. #ifndef CPP_TWIMUI_MISC_H
  33. #define CPP_TWIMUI_MISC_H
  34.  
  35. #ifndef EXEC_MEMORY_H
  36. #include <exec/memory.h>
  37. #endif
  38.  
  39. #ifndef EXEC_TYPES_H
  40. #include <exec/types.h>
  41. #endif
  42.  
  43. #ifndef _INCLUDE_STRING_H
  44. #include <string.h>
  45. #endif
  46.  
  47. #ifndef UTILITY_TAGITEM_H
  48. #include <utility/tagitem.h>
  49. #endif
  50.  
  51. class TWiMemX
  52.     {
  53.     private:
  54.         ULONG WantedSize;
  55.         ULONG WantedFlags;
  56.     public:
  57.         TWiMemX(ULONG s, ULONG f = MEMF_ANY) : WantedSize(s), WantedFlags(f) { };
  58.         TWiMemX(const TWiMemX &s) : WantedSize(s.WantedSize), WantedFlags(s.WantedFlags) { };
  59.         ULONG size() const { return(WantedSize); };
  60.         ULONG flags() const { return(WantedFlags); };
  61.     };
  62.  
  63. class TWiShare
  64.     {
  65.     private:
  66.         ULONG *counter;
  67.     public:
  68.         TWiShare();
  69.         TWiShare(const TWiShare &);
  70.         ~TWiShare();
  71.         TWiShare &operator = (const TWiShare &);
  72.         BOOL only() const { return((*counter) == 0); };
  73.     };
  74.  
  75. class TWiHelpArray
  76.     {
  77.     private:
  78.         void extend(const ULONG);
  79.         UBYTE *v;
  80.         ULONG element_size;
  81.         ULONG size;
  82.     public:
  83.         TWiHelpArray(const ULONG es, const ULONG s = 16);
  84.         TWiHelpArray(const TWiHelpArray &);
  85.         TWiHelpArray &operator= (const TWiHelpArray &);
  86.         ~TWiHelpArray() { delete [] v; };
  87.         operator APTR () const { return((APTR) v); };
  88.         void &operator[] (const ULONG);
  89.         ULONG esize() const { return(element_size); };
  90.         ULONG count() const { return(size); };
  91.     };
  92.  
  93. template <class T> class array : private TWiHelpArray
  94.     {
  95.     public:
  96.         array(const ULONG s = 16) : TWiHelpArray(sizeof(T),s) { };
  97.         ULONG count() const { return(TWiHelpArray::count()); };
  98.         T &operator[] (const ULONG i) { return((T &) TWiHelpArray::operator[] (i)); };
  99.     };
  100.  
  101. class TWiHelpArrayList : public TWiHelpArray
  102.     {
  103.     private:
  104.         ULONG top;
  105.     public:
  106.         TWiHelpArrayList(const ULONG es, const ULONG s = 16) : TWiHelpArray(es,s) { top = 0; };
  107.         TWiHelpArrayList &operator= (const TWiHelpArrayList &);
  108.         ULONG length() const { return(top); };
  109.         void &addTail() { return(TWiHelpArray::operator[](top++)); };
  110.         void &insert(const ULONG index);
  111.         void remove(const ULONG index);
  112.         void remTail();
  113.         void clear() { top = 0; };
  114.     };
  115.  
  116. class TWiHelpArrayCursor
  117.     {
  118.     private:
  119.         TWiHelpArrayList *list;
  120.         LONG pos;
  121.     public:
  122.         TWiHelpArrayCursor(TWiHelpArrayList &);
  123.         void first() { pos = 0; };
  124.         void last() { pos = list->length() - 1; };
  125.         void next();
  126.         void prev();
  127.         void &item() { return(list->operator[](pos)); };
  128.         BOOL isDone() { return(pos >= list->length()  ||  pos < 0); };
  129.     };
  130.  
  131. template <class T> class TWiArrayList : private TWiHelpArrayList
  132.     {
  133.     friend class TWiArrayCursor<T>;
  134.     public:
  135.         TWiArrayList(const ULONG s = 16) : TWiHelpArrayList(sizeof(T),s) { };
  136.         ULONG count() { return(TWiHelpArrayList::count()); };
  137.         ULONG length() const { return(TWiHelpArrayList::length()); };
  138.         T &operator= (const T &);
  139.         T &operator[] (ULONG i) { return((T &) TWiHelpArray::operator[] (i)); };
  140.         T &addTail() { return((T &) TWiHelpArrayList::addTail()); };
  141.         T &insert(const ULONG index) { return((T &) TWiHelpArrayList::insert(index)); };
  142.         void remove(const ULONG index) { TWiHelpArrayList::remove(index); }
  143.         void remTail() { TWiHelpArrayList::remTail(); };
  144.         void clear() { TWiHelpArrayList::clear(); };
  145.     };
  146.  
  147. template <class T> class TWiArrayCursor : private TWiHelpArrayCursor
  148.     {
  149.     public:
  150.         TWiArrayCursor(TWiArrayList<T> &l) : TWiHelpArrayCursor((TWiHelpArrayList &)l) { };
  151.         void first() { TWiHelpArrayCursor::first(); };
  152.         void last() { TWiHelpArrayCursor::last(); };
  153.         void next() { TWiHelpArrayCursor::next(); };
  154.         void prev() { TWiHelpArrayCursor::prev(); };
  155.         T &item() { return((T &) TWiHelpArrayCursor::item()); };
  156.         BOOL isDone() const { return(TWiHelpArrayCursor::isDone()); };
  157.     };
  158.  
  159. class TWiBuff
  160.     {
  161.     private:
  162.         UBYTE *databuff;
  163.         ULONG buffsize;
  164.         BOOL privbuff;
  165.     public:
  166.         TWiBuff(const ULONG initsize = 256);
  167.         TWiBuff(const APTR, const ULONG);
  168.         TWiBuff(const TWiBuff &);
  169.         ~TWiBuff();
  170.         TWiBuff &operator= (const TWiBuff &);
  171.         APTR buffer() const { return((APTR)databuff); };
  172.         ULONG size() const { return(databuff != NULL ? buffsize : 0); };
  173.         void doubleBuff();
  174.         void setBuffSize(ULONG);
  175.     };
  176.  
  177. class TWiStr
  178.     {
  179.     protected:
  180.         ULONG len;
  181.         TWiBuff buffer;
  182.     public:
  183.         TWiStr(const STRPTR = NULL);
  184.         TWiStr(const ULONG, const STRPTR);
  185.         TWiStr(const TWiStr &s) : len(s.len), buffer(s.buffer) { };
  186.         TWiStr(const UBYTE);
  187.         virtual ~TWiStr();
  188.         operator STRPTR() const { return((STRPTR)buffer.buffer()); };
  189.         TWiStr &operator= (const TWiStr &);
  190.         TWiStr &operator= (const STRPTR);
  191.         TWiStr &operator+= (const TWiStr &);
  192.         TWiStr &operator+= (const STRPTR);
  193.         UBYTE &operator[] (ULONG);
  194.         ULONG length() const { return(len); };
  195.         ULONG buffsize() const { return(buffer.size()); };
  196.         TWiStr left(const ULONG) const;
  197.         TWiStr right(const ULONG) const;
  198.         TWiStr mid(const ULONG, const ULONG) const;
  199.         void doubleBuff() { buffer.doubleBuff(); };
  200.         void shrinkBuff();
  201.         void setBuffSize(const ULONG);
  202.     };
  203.  
  204. TWiStr operator+ (const TWiStr &, const TWiStr &);
  205.  
  206. inline const BOOL operator== (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) == 0); };
  207. inline const BOOL operator!= (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) != 0); };
  208. inline const BOOL operator<  (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) <  0); };
  209. inline const BOOL operator>  (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) >  0); };
  210. inline const BOOL operator<= (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) <= 0); };
  211. inline const BOOL operator>= (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) >= 0); };
  212.  
  213. class ostream &operator<< (ostream &, const TWiStr &);
  214.  
  215. class istream &operator>> (istream &, TWiStr &);
  216.  
  217. class TWiStrArray
  218.     {
  219.     friend class TWiStrCursor;
  220.     private:
  221.         TWiHelpArrayList strs;
  222.     public:
  223.         TWiStrArray(STRPTR string1, ...);
  224.         TWiStrArray(STRPTR *strings = NULL);
  225.         ULONG length() const { return(strs.length()); };
  226.         STRPTR *strings();
  227.         STRPTR &operator[] (const ULONG);
  228.         void addTail(const STRPTR);
  229.         void insert(const STRPTR, const ULONG);
  230.         void remTail();
  231.         void remove(const ULONG);
  232.     };
  233.  
  234. class TWiStrCursor : public TWiHelpArrayCursor
  235.     {
  236.     public:
  237.         TWiStrCursor(TWiStrArray &);
  238.         STRPTR item();
  239.     };
  240.  
  241. class TWiTag
  242.     {
  243.     private:
  244.         struct TagItem *taglist;
  245.         void freetaglist();
  246.         struct TagItem *findtagend();
  247.     public:
  248.         TWiTag() : taglist(NULL) { };
  249.         TWiTag(const Tag tag1Type, ...);
  250.         TWiTag(const struct TagItem *);
  251.         TWiTag(const TWiTag &);
  252.         TWiTag &operator = (const TWiTag &);
  253.         ~TWiTag() { freetaglist(); };
  254.         struct TagItem *tags() const { return(taglist); };
  255.         void append(const TWiTag &);
  256.         void append(const Tag, ...);
  257.         void append(const struct TagItem *);
  258.         void set(const TWiTag &);
  259.         void set(const Tag tag1Type, ...);
  260.         void set(const struct TagItem *);
  261.         struct TagItem *find(const Tag tagType) const;
  262.         ULONG getData(const Tag tagType, const ULONG defaultData) const;
  263.         ULONG filter(const LONG logic, const Tag tagTypes[]);
  264.         ULONG filter(const LONG logic, const Tag tag1, ... );
  265.     };
  266.  
  267. class TWiTagCursor
  268.     {
  269.     private:
  270.         struct TagItem *taglist;
  271.         struct TagItem *cursor;
  272.         struct TagItem *pos;
  273.     public:
  274.         TWiTagCursor(const TWiTag &);
  275.         TWiTagCursor(struct TagItem *);
  276.         BOOL isDone() const { return(pos == NULL); };
  277.         void first();
  278.         void next();
  279.         struct TagItem *item() const { return(pos); };
  280.         Tag itemTag() const;
  281.         ULONG itemData() const;
  282.     };
  283.  
  284. class TWiFormat
  285.     {
  286.     private:
  287.         TWiBuff Buff;
  288.         TWiStr Fmt;
  289.         ULONG Index;
  290.         void TWiPutChar(UBYTE, TWiFormat *);
  291.         static void put_char(register __d0 const UBYTE, register __a3 TWiFormat *);
  292.     public:
  293.         TWiFormat(const STRPTR pFmt = NULL) : Fmt(pFmt), Buff(0UL), Index(0UL) { };
  294.         TWiFormat(const STRPTR pStr, const ULONG pLng) : Fmt(pStr), Buff(pLng), Index(0UL) { };
  295.         TWiFormat(const ULONG pLng) : Fmt(), Buff(pLng), Index(0UL) { };
  296.         TWiFormat(const TWiFormat &pFmt) : Fmt(pFmt.Fmt), Buff(pFmt.Buff), Index(pFmt.Index) { };
  297.         ~TWiFormat() { };
  298.         TWiFormat &operator= (const TWiFormat &);
  299.         void setFormat(const STRPTR pFmt) { Fmt = pFmt; };
  300.         void setBuffer(const ULONG lBuff) { Buff.setBuffSize(lBuff); };
  301.         STRPTR format(const ULONG, ...);
  302.         STRPTR format(const APTR);
  303.         STRPTR getFormat() const { return(Fmt); };
  304.         STRPTR getBuff() const { return((STRPTR)Buff.buffer()); };
  305.     };
  306.  
  307. #endif
  308.